package com.widget;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Handler;
import android.util.Log;

public class CloudParser implements ParseStreaming {
	private int count;
	private int track = 0;
	//private Handler mMsgHandler;
	private Handler mContext;
	private String fullContent="";
	Map<String,Integer> mapping;
	
	public void addApps(String directory,String append){
		Pattern permission = Pattern.compile("[-d][-rwx][-rwx][-rwx][-rwx][-rwx][-rwx][-rwx][-rwx][-rwx]");
		Pattern files = Pattern.compile("([0-9a-zA-Z.//_-]+)$");
		String []lines = fullContent.split("\r\n");
		for(int i=0;i<lines.length;i++)
		{
			Matcher permMatcher = permission.matcher(lines[i]);
			Matcher filesMatcher = files.matcher(lines[i]);
			if(permMatcher.find())
			{
				if(filesMatcher.find())
				{
					mapping.put(new String(directory+filesMatcher.group(0)+append), new Integer(0));
				}
			}
		}
	}
	@Override
	public int parseString(String content) {
		Pattern end = Pattern.compile("total size is [0-9]+  speedup is [0-9]+.[0-9]+");
		Pattern cRefused = Pattern.compile("rsync: failed to connect to [0-9]+.[0-9]+.[0-9]+.[0-9]+: Connection refused");
		Pattern nUnreachable = Pattern.compile("rsync: failed to connect to [0-9]+.[0-9]+.[0-9]+.[0-9]+: Network is unreachable");
		Pattern cTimeOut = Pattern.compile("rsync: failed to connect to [0-9]+.[0-9]+.[0-9]+.[0-9]+: Connection timed out");
		Pattern error = Pattern.compile("rsync error: error in socket IO");
		
		Matcher endM = end.matcher(content);
		Matcher cRefusedM = cRefused.matcher(content);
		Matcher nUnreachableM = nUnreachable.matcher(content);
		Matcher cTimeOutM = cTimeOut.matcher(content);
		Matcher errorM = error.matcher(content);
		fullContent += content; 
		//Check for end of successfull synchronization
		if(endM.find()){
			if(track == 0)
			{
				
				addApps("/data/app/","");
				
				fullContent = "";
				mContext.sendEmptyMessage(DATAAPPS);
			}
			else if(track == 1)
			{
				
				addApps("/system/app/","");
				
				fullContent = "";
				mContext.sendEmptyMessage(SYSTEMAPPS);
			}
			else if(track == 2)
			{
				
				addApps("/mnt/asec/","/pkg.apk");
				fullContent = "";
				mContext.sendEmptyMessage(SDAPPS);
			}
			else if(track > 2)
			{
				mContext.sendEmptyMessage(SYNCED);
			}
			track++;
			Log.d("Cloud parser","End of Rsync");
			return 0;
		}
		//if(track == count)
		//{
			//Log.d("APK parser","Finalization of Rsync");
			
			
			//track++;
			//return 0;
		//}
		
		if(cRefusedM.find()){
			mContext.sendEmptyMessage(REFUSED);
		}
		if(nUnreachableM.find()) {
			mContext.sendEmptyMessage(UNREACHABLE);
		}
		if(cTimeOutM.find()) {
			mContext.sendEmptyMessage(TIMEOUT);
		}
		if(errorM.find()) {
			mContext.sendEmptyMessage(ERROR);
			return 0;
		}
		return 1;
		
	}
	public void setMapper(Map<String,Integer> m){
		mapping = m;
	}
	@Override
	public void endMatcher(int n, Handler c) {
		count=n;
		mContext =  c;

	}

}
